package main

import (
	"context"
	"fmt"
	"google.golang.org/grpc"
	"log"
	"net"
	v1 "rpc-demo/proto/v1/pb"
)

type helloServer struct {}

// SayHello 实现 hello.proto 中对应的方法
func (*helloServer)SayHello(ctx context.Context, in *v1.HelloRequest) (*v1.HelloResponse, error) {
	return &v1.HelloResponse{
		Code:    10,
		Message: "success",
	}, nil
}

func main() {
	port := 50051
	lis, err := net.Listen("tcp", fmt.Sprintf("localhost:%d", port))
	if err != nil {
		log.Fatalf("failed to listen %v", port)
	}
	server := grpc.NewServer()
	v1.RegisterGreeterServer(server, &helloServer{})
	log.Printf("server listen at %v", lis.Addr())
	if err := server.Serve(lis); err != nil {
		log.Fatalf("failed to serve: %v", err)
	}
}
